package access;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;

public class RandomAccessTest2 {

	public static void main(String[] args) throws IOException {
		
		File file = new File( "random-access.dat" ) ;
		final byte[] buffer = new byte[ 30 ]; // 数组默认值都是 0
		final Charset charset = Charset.forName( "UTF-8" );
		
		RandomAccessFile raf = new RandomAccessFile( file , "r" ); // IOException
		
		// 将 file pointer 设置到参数对应的位置
		raf.seek( 72 );
		
		System.out.println( "file pointer : " + raf.getFilePointer() );
		
		int id = raf.readInt();// 4 bytes ==> int 
		
		raf.readFully( buffer ); // buffer.length bytes ==> buffer
		String name = new String( buffer , charset ).trim() ;
		
		char gender = raf.readChar(); // 2 bytes ==> char
		
		System.out.println( id + " , " + name + " , " + gender );
		
		
		raf.seek( 36 );
		
		id = raf.readInt();// 4 bytes ==> int 
		
		raf.readFully( buffer ); // buffer.length bytes ==> buffer
		name = new String( buffer , charset ).trim() ;
		
		gender = raf.readChar(); // 2 bytes ==> char
		
		System.out.println( id + " , " + name + " , " + gender );
		
		raf.seek( 0 );
		
		id = raf.readInt();// 4 bytes ==> int 
		
		raf.readFully( buffer ); // buffer.length bytes ==> buffer
		name = new String( buffer , charset ).trim() ;
		
		gender = raf.readChar(); // 2 bytes ==> char
		
		System.out.println( id + " , " + name + " , " + gender );
		
		raf.close();
	}

}
